package com.blog.service;

import com.blog.exception.ServiceException;
import com.blog.model.UserVo;
import com.blog.utils.CookieKit;
import com.blog.utils.RedisKit;
import com.blog.utils.StrKit;
import com.github.pagehelper.PageHelper;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;
import com.blog.dao.UserMapper;
import com.blog.model.User;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;


@Service
public class UserService {

    @Resource
    protected UserMapper userMapper;

    @Resource
    protected RedisKit redisKit;

    public User checkLogin(HttpServletResponse response,String username, String password) {
        User user = userMapper.findByName(username,password);
        if(user==null){
            throw new ServiceException("用户不存在");
        }
        if(user.getStatus() ==1){
            throw new ServiceException("用户禁止登录");
        }
        UserVo userVo = new UserVo();
        userVo.setId(user.getId());
        userVo.setUsername(user.getUsername());
        String uuid = StrKit.getRandomUUID();
        redisKit.set(uuid,userVo,3600);
        CookieKit.setSessionId2Cookie(response, uuid, "127.0.0.1", true);

        int num = user.getLoginnum() +1;
        user.setLoginnum((short)num);
        userMapper.updateByPrimaryKey(user);
        return user;
    }

    /**
     * 退出登录
     * @return
     */
    public boolean logout(HttpServletRequest request,HttpServletResponse response){
        boolean flag = true;
        String uuid = CookieKit.getSessionIdFromCookie(request,response);
        try {
            CookieKit.removeSessionIdFromCookie(response);
            redisKit.remove(uuid);
        } catch (Exception e){
            flag = false;
        }
        return false;
    }

    /**
     * 验证是否登录
     * @return
     */
    public boolean checkStatu(HttpServletRequest request,HttpServletResponse response){
        String uuid = CookieKit.getSessionIdFromCookie(request,response);
        if(uuid == null){
            return false;
        }
        Object user = redisKit.get(uuid);
        if(user == null){
            return false;
        }
        return true;
    }

    public List<User> findAll(){
        List<User> data = userMapper.findAll();
        return data;
    }


    public List<User> getAll(User user) {
        if (user.getPage() != null && user.getRows() != null) {
            PageHelper.startPage(user.getPage(), user.getRows());
        }
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if (user.getUsername() != null && user.getUsername().length() > 0) {
            criteria.andLike("username", "%" + user.getUsername() + "%");
        }
        return userMapper.selectByExample(example);
    }

    public User getUser(Integer id){
        return userMapper.selectByPrimaryKey(id);
    }

    public void editUser(User user){
        userMapper.updateByPrimaryKeySelective(user);
    }

    public void add(User user){
        User userData = userMapper.findByUsername(user.getUsername());
        if(userData!=null){
            throw new ServiceException("用户已存在");
        }
        user.setLastlogintime(new Date());
        userMapper.insertSelective(user);
    }

    public void delete(Integer id){
        try {
            userMapper.deleteByPrimaryKey(id);
        }catch (Exception e){
            throw new ServiceException("无法删除");
        }
    }

}
